<?php
	/**
	* admiao
	*/
	class  Date_MiiModel
	{		
		public function Days($startttime,$endtime,$restarr,$divmins,$d=7,$banArr,$rest_week=''){
				$unit = 24*60*60;
				$today = strtotime(date('Y-m-d'),time()); 		
				$diffSecond = $this->diffSecond(date('Y-m-d',time()).' '.$startttime,date('Y-m-d',time()).' '.$endtime);
				$nums =  $this->divTime($divmins,$diffSecond);				
				$date_cnArr = array('今天','明天','后天');		
			for ($i=0; $i < $d ; $i++) { 
				$t = $today + ($i*$unit); 
				$days = date('Y-m-d',$t);
				$startTime = $days.' '.$startttime;
				$endTime = $days.' '.$endtime;
				$restArr = 	array($days.$restarr[0],$days.$restarr[1]);	
				$TimeSlot = $this->TimeSlot($startTime,$endTime,$nums,true,$restArr,'H:i',$banArr);
				if(!empty($rest_week)){
					$iSrestDay = in_array(date('w',$t),$rest_week)?1:-1;	
				}else{
					$iSrestDay = -1;
				}	
				$week_cn = $this->week_cn(date('w',$t));
				$arr[] = array('iSrestDay'=>$iSrestDay,'week'=>date('w',$t),'week_cn'=>$week_cn,'date_cn'=>$date_cnArr[$i]?$date_cnArr[$i]:$week_cn,'date'=>$days,'TimeSlot'=>$TimeSlot);
			}
				return $arr;
		}
		private function StrtoStr($str){
			$toDay = date('Y-m-d',time());
			$i = $toDay.' '.$str;
			return $i;
		}
		private function divTime($mins=60,$second){
			$mins = $mins?$mins:60;
			return  $second/($mins*60);
		}
		private function Week_key($key){
			$week=array("A","B","C","D","E","F","G");
			return $week[$key];		
		}
		private function week_cn($k){
			$week_cn = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
			return $week_cn[$k];
		}
		public function diffSecond($startdate,$enddate){
			$diff = strtotime($enddate)-strtotime($startdate);
			return $diff;
		}
		public function TimeSlot($start, $end="", $nums = 2, $format=true,$rest=array(),$returned="Y-m-d H:i",$banArr){
		    $start = strtotime($start);
		    $end   = strtotime($end);
		    $reststart = strtotime($rest[0]);
		    $restend = strtotime($rest[1]);
		    $parts = ($end - $start)/$nums;
		    $last  = ($end - $start)%$nums;
		    // if ( $last > 0) {
		    //     $parts = ($end - $start - $last)/$nums;
		    // }
		    //return $parts;
		    for ($i=1; $i <= $nums; $i++) { 
		    	$_start = $start + $parts * ($i-1);
		        $_end  = $start + $parts * $i;
		        if(!(intval($_start)>=$reststart && intval($_start)<$restend)){
		        	$arr[] = array(intval($_start), intval($_end));
		        }
		    }
		    $len = count($arr)-1;
		    $arr[$len][1] = $arr[$len][1] + $last;
		    if ($format) {
		        foreach ($arr as $key => $value) {
		        	$d = array('start'=>$value[0],'end'=>$value[1]);
		        	$_status = $value[0]<time()?'2':'0';
		        	$status_ = $value[0]<time()?'2':'-1';
		        	//判断上午 下午
		        	if(date('A',$value[0])=='AM'){
		        		if(in_array($d,$banArr)){
		        			$am[] = array("status"=>$_status,'start'=>$value[0],'end'=>$value[1],"start_cn"=>date($returned, $value[0]),"end_cn"=>date($returned, $value[1]));
		        		}else{
		        			$am[] = array("status"=>$status_,'start'=>$value[0],'end'=>$value[1],"start_cn"=>date($returned, $value[0]),"end_cn"=>date($returned, $value[1]));
		        		}		        		
		        	}elseif(date('A',$value[0])=='PM'){
		        		if(in_array($d,$banArr)){
		        			$pm[] = array("status"=>$_status,'start'=>$value[0],'end'=>$value[1],"start_cn"=>date($returned, $value[0]),"end_cn"=>date($returned, $value[1]));
		        		}else{
		        			$pm[] = array("status"=>$status_,'start'=>$value[0],'end'=>$value[1],"start_cn"=>date($returned, $value[0]),"end_cn"=>date($returned, $value[1]));
		        		}
		        		
		        	}	
		        }
		     	$resarr = array('AM'=>$am,'PM'=>$pm);
		    	return $resarr;
		    }

		    return $arr;

		}	

	}

?>